<template>
  <div class="plain-tabs">
    <template v-for="(item,index) in list">
      <el-link
        :key="`item-${uuid()}`"
        :data-uuid="uuid()"
        class="select-item"
        :class="{'active' : index===active}"
        :underline="false"
        @click.prevent="clickfn(index)"
      >
        {{ item[label] }}
      </el-link>
    </template>
  </div>
</template>

<script>
import { uuid } from '@/utils/util'

export default {
  name: 'PlainTabs',
  props: {
    label: {
      type: String,
      default: 'text'
    },
    id: {
      type: String,
      default: 'value'
    },
    list: {
      type: Array,
      default() {
        return []
      }
    },
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      active: 0/* ,
       list: [

       {
       text: '测试1',
       value: '1-1'
       },

       {
       text: '测试2',
       value: '2-2'
       },

       {
       text: '测试3',
       value: '3-3'
       },

       {
       text: '测试4',
       value: '4-4'
       },

       {
       text: '测试5',
       value: '5-5'
       }
       ] */
    }
  },
  watch: {
    value(newVal) {
      this.list.forEach((item, index) => {
        if (item[this.id] == newVal) {
          this.active = index
        }
      })
    }
  },
  mounted() {
    this.list.forEach((v, index) => {
      if (String(v[this.id]) === String(this.value)) {
        this.active = index
      }
    })
  },
  methods: {
    clickfn(index) {
      const value = this.list[index][this.id]
      this.active = index
      // this.value = value
      this.$emit('input', value)
      this.$emit('change', value)
    },
    uuid
  }
}
</script>

<style lang="scss" scoped>
.plain-tabs {
  display: inline-block;
  vertical-align: middle;
  background: #ffffff;
  //border: 1px solid #dcdfe6;
  //border-radius: 4px;
  height: 40px;
  line-height: 40px;

  &:hover {
    border-color: #c0c4cc;
  }

  .select-item {
    cursor: pointer;
    color: #909399;
    padding: 7px 12px;
    line-height: 24px;
    display: inline-block;
    vertical-align: middle;
    margin-right: 30px;
    font-weight: 600;

    &.active {
      color: #0079ff;
      border-bottom: 2px solid #1989fa;
    }
  }

  .select-split {
    display: inline-block;
    vertical-align: middle;
    border-left: 1px solid #dcdfe6;
    height: 15px;
    width: 1px;
    margin-right: 5px;
  }
}
</style>
